/*
	Author: Genesis

	Description:
		使小队进入附近的静态武器

	Parameter(s):
		0: GROUP - 受影响的群体

	Returns:
		NOTHING
	
	Example1: groupAlpha call VCM_fnc_ArmStatics;
	
	k1编辑 2021.9.12
*/

private _leader = leader _this;
private _weaps = nearestObjects [_leader, ["StaticWeapon"], 150];
private _unitArray = units _this;
if (count _weaps < 0) exitWith {};
private _assignedPairs = []; //Static weapon - Gunner pair

{
	if (alive _x && {isNull assignedGunner _x}) then {
		private _unit = [_unitArray,_x,true,"W1"] call VCM_fnc_ClstObj; //从对象数组中查找最近的对象

		//VCM_fnc_ClstObj returns [0,0,0] if nothing found
		if (_unit isEqualTo [0,0,0]) exitWith {};

		if ([_unit, "VIEW", _x] checkVisibility [eyePos _unit, eyePos _x] > 0 && {isNull objectParent _unit} && {_unit distance2D _x < 100}) then {
			_assignedPairs pushback [_unit,_x];
			//模拟findIf命令特性的写法
			{if(_x isEqualTo _unit)exitWith{_unitArray deleteAt _forEachIndex}} forEach _unitArray;
			//_unitArray deleteAt (_unitArray findIf {_x isEqualTo _unit}); //arma3 v1.82
		};
	};
} forEach _weaps;

if (count _assignedPairs isEqualTo 0) exitWith {};

{
	_x spawn {
		params ["_unit","_weap"];
		if (isNull assignedGunner _weap) then {
			_unit enableAI "PATH"; //Overwrites garrison
			_unit assignAsGunner _weap;
			_time = 0;
			_dist2 = 0;
			_Waiting = time + 300; //
			while {alive _unit && {time < _Waiting} && {_unit distance _weap > 5 || {[_unit, "VIEW", _weap] checkVisibility [eyePos _unit, eyePos _weap] isEqualTo 0} || {lifeState _unit == "INCAPACITATED"} || {!(_unit getVariable ["k1_Only_Damage_EH", true])} || {animationState _unit in ["unconscious","unconsciousoutprone"]}}} do {
				if (time > _time || {_unit distance _weap > _dist2}) then {
					_pos = if(surfaceIsWater getPosASL _weap)then[{getPosASL _weap},{getPosATL _weap}];
					_unit allowFleeing 0;
					_unit doMove _pos;
					_unit moveTo _pos;
					_dist2 = _unit distance _weap;
					_time = time + 30;
				};
				sleep 2;
			}; //4
			if (!alive _unit || {!(time < _Waiting)} || {alive gunner _weap}) exitWith {unassignVehicle _unit; _unit leaveVehicle _weap}; //
			
			if ((vectorUp _weap select 2 < .6 || {getPos _weap select 2 < -.3}) && {alive _weap}) then {
				_enemy = _unit findNearestEnemy _unit;
				if (!isNull _enemy) then {_weap setDir (_weap getDir _enemy)};
				_weap setVehiclePosition [_weap,[],0,"CAN_COLLIDE"];
				sleep .1;
			}; //扶正武器
			
			[_unit] orderGetIn true; //离近后会瞬移进入！
			_unit moveInGunner _weap;
			
			[_unit,_weap] spawn { //操作固定式武器多久
				params ["_unit","_weap"];
				sleep 8; //
				private _staticGreen = true;
				private _statictime = VCM_STATICARMT;
				private _behaviour = (random 100 < 30); //
				while {_staticGreen && {alive _unit} && {alive _weap} && {gunner _weap isEqualTo _unit} && {_unit distance2D (leader (group _unit)) < 500} && {(_behaviour && {behaviour _unit in ["AWARE", "COMBAT"]}) || {behaviour _unit isEqualTo "COMBAT"}}} do
				{
					sleep 5;
					private _enemy = _unit findNearestEnemy _unit;
					if (!isNull _enemy) then [{
						private _cansee = [vehicle _unit, "VIEW", _enemy] checkVisibility [eyePos vehicle _unit, eyePos _enemy];
						_statictime = if(_cansee > 0)then[{_statictime + 3},{_statictime - 5}];
					},{
						_statictime = _statictime - 5;
					}];
					if (_statictime < 1) then {_staticGreen = false};
				};				
				
				unassignVehicle _unit;
				_unit leaveVehicle _weap;
				doGetOut _unit;
			};
		};		
	};	
} forEach _assignedPairs;